package com.sitech.portal.system.dao.ibatis;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.sitech.ibnms.core.BaseIbnmsDao;
import com.sitech.ibnms.core.IPage;
import com.sitech.ibnms.core.PageForm;
import com.sitech.ibnms.util.LabelValueBean;
import com.sitech.portal.system.dao.ISysDeptDao;
import com.sitech.portal.system.domain.TblSYS_DEPT;

public class SysDeptDaoImpl extends BaseIbnmsDao implements ISysDeptDao {

	public void add(TblSYS_DEPT tblSYS_DEPT) {
		this.insert("insertForTB_SYS_DEPT", tblSYS_DEPT);
	}

	public void update(TblSYS_DEPT tblSYS_DEPT) {
		this.update("updateForTB_SYS_DEPT", tblSYS_DEPT);
	}

	public void delete(TblSYS_DEPT tblSYS_DEPT) {
		this.delete("deleteForTB_SYS_DEPT", tblSYS_DEPT);
	}

	public TblSYS_DEPT findDepartByPK(String s) {
		return (TblSYS_DEPT) this
					.querySingle("findDepartByPK", s);
	}

	public List findRootDepartByCompId(String strCompId) {

		if (strCompId == null || strCompId.equals("")) {
			return this.query(
					"findRootDepartByCompIdForTB_SYS_DEPT", null);
		} else {
			TblSYS_DEPT obj = new TblSYS_DEPT();
			obj.setCOMP_ID(strCompId);

			return this.query(
					"findRootDepartByCompIdForTB_SYS_DEPT", obj);
		}
	}

	public List findRootDepartByCompIdAndManageUnitID(String strCompId,
			String manage_unit_id) {

		if (strCompId == null || strCompId.equals("")) {
			if ("ALL".equals(manage_unit_id))
				return this.query(
						"findRootDepartByCompIdForTB_SYS_DEPT", null);
			else {
				TblSYS_DEPT obj = new TblSYS_DEPT();
				obj.setMANAGE_UNIT_ID(manage_unit_id);
				return this.query(
						"findRootDepartByCompIdForTB_SYS_DEPT", null);
			}
		} else {
			if ("ALL".equals(manage_unit_id)) {
				TblSYS_DEPT obj = new TblSYS_DEPT();
				obj.setCOMP_ID(strCompId);
				return this.query(
						"findRootDepartByCompIdForTB_SYS_DEPT", obj);
			} else {
				TblSYS_DEPT obj = new TblSYS_DEPT();
				obj.setCOMP_ID(strCompId);
				obj.setMANAGE_UNIT_ID(manage_unit_id);
				return this.query(
						"findRootDepartByCompIdForTB_SYS_DEPT", obj);
			}
		}
	}

	/**
	 * 根据DEPT_ID 查询本部门人员信息
	 * 
	 * @param DeptId
	 *            20070905 add
	 * @return
	 */
	public List findMgrIdEmployee(String DeptId) {

		String sql = "select * from tb_sys_loginmsg where dept_id like '"
				+ DeptId + "%' order by login_id";
		return this.query("findMgrIdEmployee", sql);
	}

	/**
	 * 查询子部门
	 */
	public List findBySuperDeptId(String strSuperDeptId) {

		if (strSuperDeptId == null || strSuperDeptId.equals("")) {
			strSuperDeptId = "0";
		} 
		TblSYS_DEPT obj = new TblSYS_DEPT();
		obj.setSUPER_DEPT_ID(strSuperDeptId);
		return this
				.query("findByListForTB_SYS_DEPT", obj);
	}
	
	public List findDeptNodeInfo(String deptId) {

		TblSYS_DEPT obj = new TblSYS_DEPT();
		obj.setDEPT_ID(deptId);
		return this
				.query("findByListForTB_SYS_DEPT_ID", obj);
	}

	// liuwei add 20091118
	public List findByDeptId(String DeptId) {
		TblSYS_DEPT obj = new TblSYS_DEPT();
		obj.setDEPT_ID(DeptId);
		return this.query("findByListByDeptID", obj);
	}

	public int findCountBySuperDeptId(String deptId) {
		Integer size = null;
		if (deptId == null || deptId.equals("")) {
			size = (Integer) this.querySingle(
					"findCountBySuperDeptId", null);
		} else {
			TblSYS_DEPT obj = new TblSYS_DEPT();
			obj.setSUPER_DEPT_ID(deptId);
			size = (Integer) this.querySingle(
					"findCountBySuperDeptId", obj);
		}
		if (size == null) {
			return 0;
		} else {
			return size.intValue();
		}
	}

	public List findForOptions() {
		List list = new ArrayList();
		List deptList = findDepartForBulletin();
		if (null != deptList) {
			for (int i = 0; i < deptList.size(); i++) {
				HashMap dept = (HashMap) deptList.get(i);
				list.add(new LabelValueBean((String) dept.get("DEPT_NAME"),
						(String) dept.get("DEPT_ID")));
			}
		}
		return list;
	}

	public List findDepartForBulletin() {
		return (List) this.query("findDepartForBulletin",
					null);
	}

	// 查找部门负责人
	public HashMap findDepartAdminByDeptId(String DEPT_ID) {
		return (HashMap) this.querySingle(
					"findDepartAdminByDeptId", DEPT_ID);
	}

	// 查找部门所属区域属性
	public HashMap findDepartAreaByDeptId(String DEPT_ID) {
		return (HashMap) this.querySingle(
					"findDepartAreaByDeptId", DEPT_ID);
	}

	public void delForTB_SYS_MODULE(String pre_module_id, String module_name) {
		String mySqlCount = "DELETE FROM tb_sys_module WHERE module_id LIKE '"
				+ pre_module_id + "%' AND module_name = '" + module_name + "'";
		this.delete("delForTB_SYS_MODULE", mySqlCount);
	}

	public List findRootDepartHavaChildByCompId(String COMP_ID) {
		return this.query("findRootDepartHavaChildByCompId",
					COMP_ID);
	}

	public List findDepartHavaChildBySuperDeptId(String SUPER_DEPT_ID) {
		return this.query("findDepartHavaChildBySuperDeptId",
					SUPER_DEPT_ID);
	}
	
	public IPage findListByParentId(TblSYS_DEPT tblSYS_DEPT,PageForm pageForm){
		return this.findPage("findDeptListByParentId", tblSYS_DEPT, pageForm);
	}

	@Override
	public String findMaxDeptIdBySuperDeptId(String strSuperDeptId) {
		String super_dept_id = strSuperDeptId;
		Object obj = this.querySingle("findMaxDeptIdBySuperDeptId", super_dept_id);
		if(obj==null)
			return null;
		return obj.toString();
	}
	//查找用户负责的部门
	public List<HashMap> findDeptByMgrId(String mgrId) {
		String mgr_id = mgrId;
		return this.query("findDeptByMgrId", mgr_id);
	}
}
